<template>
  <div class="login" ref="login">
    <van-form>
      <van-field
      v-model="form.tel"
      type="tel"
      name="pattern"
      placeholder="请输入手机号"
      :error-message="errorMessage"
      :rules="[{ pattern }]" />

      <!-- @input="validate" -->
      <van-field
      v-model="form.password"
      type="password"
      placeholder="请输入密码"
      :error-message="errorPwdMsg"
      />
      <van-button class="btn" @click="logIn(form)">登录</van-button>
    </van-form>

    <p>忘记密码？</p>

    <div class="second-try">
      <div @click="$router.push('/loginphone')">验证码登录</div>
      <div @click="$router.push('/register')">新用户注册</div>
    </div>

    <!-- 勾选协议 -->
    <!-- <div class="agreement">
      <van-checkbox-group v-model="result">
        <van-checkbox name="value">已阅读并同意<a href="#">《租车协议》</a></van-checkbox>
      </van-checkbox-group>
    </div> -->
  </div>
</template>

<script>
import { getUserList } from "../api/index.js";
export default {
  data() {
    return {
      form: {
        tel: "",
        password: ""
      },
      value: '',
      result: [],
      errorMessage: '', // 手机号的错误信息
      errorPwdMsg: '', // 密码的错误信息
      pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
    };
  },
  mounted() {
    this.getHeight();
  },
  computed: {
  },
  methods: {
    // 验证手机号
    validate() {
      this.logIn(this.form.tel);
    },
    getHeight() {
      this.$refs.login.style.height =
        document.documentElement.clientHeight + "px";
    },
    // 登录事件
    logIn(params) {
      getUserList(
        {
          "username": params.tel,
          "password": this.$md5(params.password)
        }
      ).then(res => {
        if(res.data.data.length !== 0 && params.tel === res.data.data[0].username) {
          if(this.$md5(params.password) === '') {
            this.errorPwdMsg = '请输入密码';
          }else if(this.$md5(params.password) === res.data.data[0].password) {
            // 将用户数据存入cookies中
            this.$cookie.set('username', res.data.data[0].username);
            this.$cookie.set('userId', res.data.data[0].id);
            this.$cookie.set('password', this.$md5(res.data.data[0].password));

            this.$router.push('/msite');
          }else {
            this.errorPwdMsg = '密码错误'
          }
          this.errorMessage = '';
        }else {
          this.errorMessage = '请正确输入手机号1';
        }
      }).catch(err => {
        console.log('查不到用户数据', err);
      })
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 0;
  background-color: rgb(236, 236, 236);
  padding-top: 100px;
  box-sizing: border-box;
  .van-form {
    width: 100%;
    display: flex;
    flex-flow: column wrap;
    justify-content: center;
    padding: 0 10%;
    box-sizing: border-box;
  }
  .van-field {
    width: 100%;
    margin: 0 auto;
    margin-bottom: 10px;
    border-radius: 10px;
  }
  p {
    text-align: center;
    font-size: 8px;
    margin: 20px 0;
  }
  .btn {
    width: 200px;
    height: $height;
    line-height: $height;
    text-align: center;
    color: #fff;
    background-color: $color;
    border-radius: 20px;
    margin: 0 auto;
  }

  .second-try {
    width: 80%;
    margin: 10px auto;
    display: flex;
    justify-content: space-between;
    font-size: 12px;
  }

  .agreement {
    width: 90%;
    position: absolute;
    bottom: 20px;
    left: 15px;
    box-sizing: border-box;
  }
}
</style>